<template>
  <div class="form-container">
    <el-form :model="form" label-width="80px">
      <el-form-item label="名称">
        <el-input
          v-model="form.name"
          autocomplete="off"
          placeholder="请输入名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="form.sort" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { saveOrUpdate } from '@/services/resource-category'
export default Vue.extend({
  name: 'CategoryForm',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    category: {
      type: Object,
      default: () => null
    }
  },
  data () {
    return {
      form: {
        name: '',
        sort: 0
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.form = this.category
    }
  },
  methods: {
    onCancel () {
      this.$emit('on-cancel')
    },
    async onSubmit () {
      const { data } = await saveOrUpdate(this.form as FromServer.ResourceCategoryItem) as any
      if (data) {
        this.$message.success('操作成功')
      } else {
        this.$message.error('操作失败')
      }
      this.$emit('on-submit')
    }
  }
})
</script>

<style lang="scss" scoped>
.form-container {
  width: 80%;
  margin: 0 auto;
}

.dialog-footer {
  text-align: right;
}
</style>
